import {baseUrl, devMode} from "../data";
import {request} from "../functions";
import CustomerId from "../model/CustomerId";
import ProfileData from "../model/ProfileData";
import {CustomerApiResult} from './CustomerApiResult';
import CustomerGroupId from "../model/CustomerGroupId";
import ProfileDataApiResult from "../model/ProfileDataApiResult";



/**
 * Returns the data for the customer profile page.
 */
export default async function getCustomerProfile(id: CustomerId): Promise<CustomerApiResult<ProfileData, string>> {
    try {
        const body = {
            customerId: id.value(),
        };

        const url = `${baseUrl}/admin/api/customer-profile`;
        const response = await request<ProfileDataApiResult>(url, 'POST', body);
        const profileData = mapProfileDataApiResultToProfileData(response);
        
        profileData.activeCountries.forEach((country: any) => {
            if (Object.keys(country).includes('zones') && country.zones.length) {
                // adds an empty element to the first array position
                country.zones.unshift({id: 0, name: ''});
            }
        });

        return {
            success: true,
            value: {
                customer: profileData.customer,
                customerGroups: profileData.customerGroups,
                customerMemos: profileData.customerMemos,
                address: profileData.address,
                history: profileData.history,
                disallowedPaymentMethods: profileData.disallowedPaymentMethods,
                disallowedShippingMethods: profileData.disallowedShippingMethods,
                statistics: profileData.statistics,
                currency: profileData.currency,
                products: profileData.products,
                logAdminActivities: profileData.logAdminActivities,
                configurations: profileData.configurations,
                userConfigurations: profileData.userConfigurations,
                userId: profileData.userId,
                activeCountries: profileData.activeCountries,
                newsletterSubscriptionStatus: profileData.newsletterSubscriptionStatus,
            }
        };
    } catch (e) {
        if (devMode) {
            console.error(e);
        }

        return {success: false, error: "Error while fetching customer"};
    }
}
function mapProfileDataApiResultToProfileData(response: ProfileDataApiResult): ProfileData {
    return {
        customer: {
            id: new CustomerId(response.customer.id),
            customerGroup: new CustomerGroupId(response.customer.customerGroup),
            isGuestAccount: response.customer.isGuestAccount,
            isFavorite: response.customer.isFavorite,
            personalInformation: response.customer.personalInformation,
            contactInformation: response.customer.contactInformation,
            businessInformation: response.customer.businessInformation,
            credit: response.customer.credit,
            addresses: response.customer.addresses,
            disallowedPaymentMethods: response.customer.disallowedPaymentMethods,
            disallowedShippingMethods: response.customer.disallowedShippingMethods,
        },
        customerGroups: response.customerGroups,
        customerMemos: response.customerMemos,
        address: response.address,
        history: response.history,
        disallowedPaymentMethods: response.disallowedPaymentMethods,
        disallowedShippingMethods: response.disallowedShippingMethods,
        statistics: response.statistics,
        currency: response.currency,
        products: response.products,
        logAdminActivities: response.logAdminActivities,
        configurations: response.configurations,
        userConfigurations: response.userConfigurations,
        userId: response.userId,
        activeCountries: response.activeCountries,
        newsletterSubscriptionStatus: response.newsletterSubscriptionStatus,
    };
}
